package com.cuit.zuke.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.product.client.DictFeignClient;
import com.cuit.zuke.product.mapper.StockMapper;
import com.cuit.zuke.product.mapper.WarehouseMapper;
import com.cuit.zuke.product.model.entity.Stock;
import com.cuit.zuke.product.model.entity.Warehouse;
import com.cuit.zuke.product.model.vo.WarehouseVO;
import com.cuit.zuke.product.service.StockService;
import com.cuit.zuke.product.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {


    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    @Cacheable(value = "warehouse")  //返回的集合会放到缓存中
    public Page<WarehouseVO> findPage(Integer page, Integer limit) {
        Page<Warehouse> warehousePage = new Page<>(page,limit);
        Page<Warehouse> warehousePage1 = baseMapper.selectPage(warehousePage, null);
        List<Warehouse> records = warehousePage1.getRecords();
        // Warehouse -> WarehouseVO
        List<WarehouseVO> warehouseVOList = new ArrayList<>();
        for (Warehouse warehouse : records) {
            WarehouseVO warehouseVO = new WarehouseVO();
            String fullLoc = getFullLocNameById(warehouse.getId());
            warehouseVO.setId(warehouse.getId());
            warehouseVO.setFullLoc(fullLoc);
            warehouseVO.setSupervisor(warehouse.getSupervisor());
            warehouseVO.setTel(warehouse.getTel());
            warehouseVO.setName(warehouse.getName());
            warehouseVOList.add(warehouseVO);
        }
        Page<WarehouseVO> warehouseVOPage = new Page<>(warehousePage1.getCurrent(),warehousePage1.getSize());
        warehouseVOPage.setRecords(warehouseVOList);
        warehouseVOPage.setTotal(warehousePage1.getTotal());
        return warehouseVOPage;
    }

    @Override
    public List<Warehouse> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public String getNameById(Integer id) {
        Warehouse warehouse = baseMapper.selectById(id);
        return warehouse.getName();
    }

    @Override
    public String getFullLocNameById(Integer id) {
        Warehouse warehouse = baseMapper.selectById(id);
        String fullLoc = setFullLoc(warehouse.getProvinceCode(), warehouse.getCityCode(), warehouse.getLocCode()) + warehouse.getDetailedAddress();
        return fullLoc;
    }

    private String setFullLoc(Long provinceCode, Long cityCode, Long locCode){
        String fullName = dictFeignClient.getNameById(provinceCode) + "-" +
                dictFeignClient.getNameById(cityCode) + "-" +
                dictFeignClient.getNameById(locCode)+ "-";
        return fullName;
    }

}
